﻿using Aribaccio;
using Aribaccio.Device;
using BDPAutodetect.Device;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// TargetSlaveEditorDialog.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class TargetSlaveEditorDialog : Window, IDialog<MTargetSlave>
    {
        /// <summary>
        /// 是否可见
        /// </summary>
        [ObservableProperty]
        protected bool isEnabledSlave = false;
        /// <summary>
        /// 显示标题
        /// </summary>
        [ObservableProperty]
        protected string titleName = "指标编辑";
        /// <summary>
        /// 显示状态信息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 工位集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MStation> stationItems;
        /// <summary>
        /// 从机设备集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MSlave> slaveItems;
        /// <summary>
        /// 指标集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MTarget> targetItems;
        /// <summary>
        /// 校验类型集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<DeviceCheck> checkModeItems;
        /// <summary>
        /// 数据类型集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<DataType> dataTypeItems;
        /// <summary>
        /// 数据单位集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<DataUnit> dataUnitItems;
        /// <summary>
        /// 状态集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<ExecuteStatus> statusItems;

        /// <summary>
        /// 当前指标从机设备协议
        /// </summary>
        [ObservableProperty]
        protected MTargetSlave targetSlaveData;
        /// <summary>
        /// 原数据
        /// </summary>
        protected MTargetSlave SourceSlaveData { get; set; }
        /// <summary>
        /// 返回结果信息
        /// </summary>
        public MTargetSlave? DialogResultData { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public TargetSlaveEditorDialog(MTargetSlave argSourceSlaveData, ObservableCollection<MStation> agrStations, ObservableCollection<MSlave> argSlaves, ObservableCollection<MTarget> argTargets)
        {
            InitializeComponent();
            this.CheckModeItems = new ObservableCollection<DeviceCheck>(Enum.GetValues<DeviceCheck>());
            this.DataTypeItems = new ObservableCollection<DataType>(Enum.GetValues<DataType>());
            this.DataUnitItems = new ObservableCollection<DataUnit>(Enum.GetValues<DataUnit>());
            this.StatusItems = new ObservableCollection<ExecuteStatus>(Enum.GetValues<ExecuteStatus>());
            this.StationItems = agrStations;
            this.SlaveItems = argSlaves;
            this.TargetItems = argTargets;
            this.SourceSlaveData = argSourceSlaveData;
            this.TargetSlaveData = argSourceSlaveData.CloneDeep();
            this.IsEnabledSlave = argSourceSlaveData.Target.TargetCategory != DeviceType.Customer;
            this.DataContext = this;
        }

        /// <summary>
        /// 确认选择事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmOk()
        {
            //校验
            if (!this.Check()) return;
            this.DialogResult = true;
            this.DialogResultData = this.TargetSlaveData;
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmCancel()
        {
            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 校验
        /// </summary>
        /// <returns></returns>
        protected bool Check()
        {
            if (this.SourceSlaveData == null) return true;
            if (this.SourceSlaveData.SlaveCommand != this.TargetSlaveData.SlaveCommand)
            {
                return true;
            }
            if (this.SourceSlaveData.CheckCommand != this.TargetSlaveData.CheckCommand)
            {
                return true;
            }
            if (this.SourceSlaveData.SlaveAddress != this.TargetSlaveData.SlaveAddress)
            {
                return true;
            }
            if (this.SourceSlaveData.SlaveFuncCode != this.TargetSlaveData.SlaveFuncCode)
            {
                return true;
            }
            if (this.SourceSlaveData.RegisterAddress != this.TargetSlaveData.RegisterAddress)
            {
                return true;
            }
            if (this.SourceSlaveData.CheckMode != this.TargetSlaveData.CheckMode)
            {
                return true;
            }
            if (this.SourceSlaveData.DataType != this.TargetSlaveData.DataType)
            {
                return true;
            }
            if (this.SourceSlaveData.DataUnit != this.TargetSlaveData.DataUnit)
            {
                return true;
            }
            if (this.SourceSlaveData.DisplayUnit != this.TargetSlaveData.DisplayUnit)
            {
                return true;
            }
            if (this.SourceSlaveData.DataPrecision != this.TargetSlaveData.DataPrecision)
            {
                return true;
            }
            if (this.SourceSlaveData.SendWaitStatus != this.TargetSlaveData.SendWaitStatus)
            {
                return true;
            }
            if (this.SourceSlaveData.DetectWaitStatus != this.TargetSlaveData.DetectWaitStatus)
            {
                return true;
            }
            if (this.SourceSlaveData.PermissionStatus != this.TargetSlaveData.PermissionStatus)
            {
                return true;
            }
            this.StatusMsg = "编辑未改变，可以直接点击取消";
            return false;
        }
    }
}
